package org.edu.modules.research.rwork.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.edu.common.base.BasePageDto;
import org.edu.common.base.BaseServiceImpl;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.utils.ConvertUtils;
import org.edu.common.base.BaseServiceImpl;
import org.edu.modules.research.rbase.entity.VeJkyOrganizeUser;
import org.edu.modules.research.rbase.service.VeJkyOrganizeUserService;
import org.edu.modules.research.rwork.entity.VeJkyMeetRemindUser;
import org.edu.modules.research.rwork.mapper.VeJkyMeetRemindMapper;
import org.edu.modules.research.rwork.entity.VeJkyMeetRemind;
import org.edu.modules.research.rwork.dto.VeJkyMeetRemindDto;
import org.edu.modules.research.rwork.service.VeJkyMeetRemindService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.edu.modules.research.rwork.service.VeJkyMeetRemindUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 实现类
 */
@Service
public class VeJkyMeetRemindServiceImpl extends BaseServiceImpl<VeJkyMeetRemind, VeJkyMeetRemindMapper> implements VeJkyMeetRemindService {


    @Autowired
    public VeJkyMeetRemindUserService meetRemindUserService;

    public IPage<VeJkyMeetRemindDto> pageDto(BasePageDto page, @Param(Constants.WRAPPER) Wrapper queryWrapper){
        return mapper.pageDto(page,queryWrapper);
    }
    /**
    * 查询实体类
    */
    public VeJkyMeetRemindDto findDtoById(String id) {
        VeJkyMeetRemind entity = super.findById(id);
        VeJkyMeetRemindDto dto = ConvertUtils.convertToDto(entity, VeJkyMeetRemindDto.class);

        LambdaQueryWrapper<VeJkyMeetRemindUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(VeJkyMeetRemindUser::getJkyUserId);
        lambdaQueryWrapper.eq(VeJkyMeetRemindUser::getRemindId,dto.getId());
        List<VeJkyMeetRemindUser> veJkyOrganizeUsers = meetRemindUserService.listByWrapper(lambdaQueryWrapper);
        List<Integer> userIds = new ArrayList<>();
        for (VeJkyMeetRemindUser user: veJkyOrganizeUsers) {
            userIds.add(user.getJkyUserId());
        }
        dto.setUserIds(userIds);


        return dto;
    }

    /**
    * 新增或者修改
    */
    public void insertOrUpdate(VeJkyMeetRemindDto dto,Boolean isAdd){
        if(isAdd){
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            dto.setCreateUid(sysUser.getId());
            dto.setCreateName(sysUser.getRealname());
            dto.setCreateTime(new Date());
            super.insert(dto);
        }else{
            meetRemindUserService.deleteByPid(dto.getId()+"");
            super.update(dto);
        }
        List<VeJkyMeetRemindUser> users = new ArrayList<>();
        for (Integer userId:dto.getUserIds()) {
            VeJkyMeetRemindUser user = new VeJkyMeetRemindUser();
            user.setJkyUserId(userId);
            user.setRemindId(dto.getId());
            users.add(user);
        }
        meetRemindUserService.insert(users);

    }

}
